package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.EmploymentInfo;
import com.project.entity.OrgInfo;
import com.project.entity.StudentInfo;
import com.project.myBeans.EmploymentInfoDetail;
import com.project.myBeans.R;
import com.project.service.EmploymentInfoService;
import com.project.service.OrgInfoService;
import com.project.service.StudentInfoService;
import com.project.service.UserService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 17:22
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/employmentinfo")
@Api(description = "就业信息处理", value = "employmentinfo")
public class EmploymentInfoController extends BaseService{
    @Autowired
    EmploymentInfoService employmentInfoService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * TateBrown 2018年5月26日
     *
     * @param employmentInfo
     * @return
     */
    @ApiOperation("模糊搜索+条件 分页显示")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody EmploymentInfo employmentInfo){
        try{
            if (employmentInfo.getPageIndex() > 0)
                employmentInfo.setPageIndex(employmentInfo.getPageIndex() - 1);
            List<EmploymentInfo> employmentInfos = employmentInfoService.findByCondition(employmentInfo);
            List<EmploymentInfoDetail> employmentInfoDetails = new ArrayList<>();
            for (EmploymentInfo employmentInfo1 : employmentInfos) {
                employmentInfoDetails.add(employmentInfoService.findDetailById(employmentInfo1.getId()));
            }
            return R.ok("查询成功").put("data",employmentInfoDetails).put("total", MyPageHelper.getTotal(employmentInfoDetails));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param employmentInfo
     * @return
     */
    @ApiOperation("添加信息")
    @PostMapping("/add")
    public R add(@RequestBody EmploymentInfo employmentInfo){
        try{
            //将此学生之前的所有就业信息置为无效
            EmploymentInfo employmentInfo1 = new EmploymentInfo();
            employmentInfo1.setUserid(employmentInfo.getUserid());
            employmentInfo1.setValid(2);//无效
            employmentInfoService.updateBatchByUserId(employmentInfo1);
            //添加新的
            employmentInfoService.add(employmentInfo);
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setUserid(employmentInfo.getUserid());
            studentInfo.setEmployed(2);//已就业
            studentInfoService.modifyByUserIdSelective(studentInfo);
            return R.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("按照id查找信息")
    @GetMapping("/findDetailById/{id}")
    public R findDetailById(@PathVariable("id") Integer id){
        try{
            return R.ok("查找成功").put("data",employmentInfoService.findDetailById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

    /**
     * TateBrown 2018年5月26日
     *
     * @param studentInfo
     * @return
     */
    @ApiOperation("每个学生均显示一条就业信息，若无则显示为空")
    @PostMapping("/findBatchByCondition")
    public R findBatchByCondition(@RequestBody StudentInfo studentInfo){
        try{
            List<JSONObject> jsonObjectList = new ArrayList<>();
            studentInfo.setPageIndex(studentInfo.getPageIndex() - 1);
            List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
            for (StudentInfo studentInfo1 : studentInfoList){
                JSONObject jsonObject = new JSONObject();
                //查询出学生信息详细记录 （用户表 + 学生表 + 系/专业/班级表）
                jsonObject.put("studentInfoDetail",studentInfoService.findDetailByStudentInfo(studentInfo1));
                //就业信息表信息 查询有效的就业信息即valid = 1
                EmploymentInfo employmentInfo = employmentInfoService.findOneByUserAndValid(studentInfo1.getUserid());
                if (employmentInfo != null) {
                    jsonObject.put("employmentInfo", employmentInfo);
                    //单位信息
                    if (employmentInfo.getId() != null) {
                        OrgInfo orgInfo = orgInfoService.findByid(employmentInfo.getEmployerid());
                        jsonObject.put("orgInfo", orgInfo);
                    }
                }
                jsonObjectList.add(jsonObject);
            }
            return R.ok("查询成功").put("data",jsonObjectList).put("total",MyPageHelper.getTotal(jsonObjectList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

}
